package com.teame.readbaby;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.teame.readbaby.parser.LogisticsParser;
import com.teame.readbaby.util.ConstantValue;
import com.teame.readbaby.vo.Logistics;

public class LogisticsActivity extends BaseActivity {

	protected static final String TAG = "LogisticsActivity";
	private View logistics;
	private TextView expressway;
	private TextView logisticscorp;
	private TextView logisticsid;
	private TextView logistic_track;
	private TextView logistics_null_text;
	
	private ListView logistic_track_content;
	private TextView head_back_text;
	private String orderId;
	
	@Override
	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
	}

	public void onClick1(View v) {
		if(v.getId() == R.id.head_back_text){
			//TODO 跳转回订单详情列表
			Intent intent = new Intent(context, MyOrderDetailActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			startActivity(intent);
		}
	}

	@Override
	protected void findViewById() {
		expressway = (TextView) findViewById(R.id.expressway);
		logisticscorp = (TextView) findViewById(R.id.logisticscorp);
		logisticsid = (TextView) findViewById(R.id.logisticsid);
		logistic_track = (TextView) findViewById(R.id.logistic_strack);
		logistics_null_text = (TextView) findViewById(R.id.logistics_null_text);
		head_back_text = (TextView) findViewById(R.id.head_back_text);
		
		logistic_track_content = (ListView) findViewById(R.id.logistic_strack_content);
		
		Intent intent = new Intent();
		orderId = intent.getStringExtra("orderId");
	}

	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.logistics_check_activity);
	}

	@Override
	protected void processLogic() {
		String url = ConstantValue.HOST + ConstantValue.LOGISTICS;
		Map<String, String> params = new HashMap<String, String>();
		// TODO orderId从订单详情界面传入
		params.put("orderId", orderId);
		this.getDataFromServer(url, params, new LogisticsParser(),
				new DataCallback<Logistics>() {

					@Override
					public void processData(Logistics paramObject,
							boolean paramBoolean) {
						if (!paramBoolean || paramObject == null) {// 获取失败
							showNoLogistics();
						} else {// 获取成功
							expressway.setText(paramObject.getExpressway());
							logisticsid.setText(paramObject.getLogisticsid());
							logisticscorp.setText(paramObject.getLogisticscorp());
							
							ArrayList<String> logistic_tracks = paramObject.getList();
							String string = getResources().getString(R.string.logistic_strack);
							String replace = StringUtils.replace(string, "TODO", paramObject.getLogisticscorp());
							logistic_track.setText(replace);
							
							//TODO 还剩下对ListView的适配 ==> 直接使用数组适配器
							logistic_track_content.setAdapter(new ArrayAdapter(context, R.layout.logistics_check_activity_item, logistic_tracks));
						}
					}

				});
		// 4,回调函数
	}
	
	private void showNoLogistics() {
		logistics_null_text.setVisibility(View.VISIBLE);
	}

	@Override
	protected void setListener() {
		head_back_text.setOnClickListener(this);
	}

	@Override
	protected void onClickEvent(View paramView) {
		onClick1(paramView);
	}
}
